﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;


namespace TInventory
{
    public partial class AddEditSupplier : Form
    {
        TIEntities dbContext;
        public DataGridViewRow selectedRecord = null;

        public AddEditSupplier()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (selectedRecord == null) // Add New Record
            {
                try
                {
                    Supplier sup = new Supplier();
                    
                    sup.CompanyName = this.textBoxCompanyName.Text;
                    sup.ContactPersonName = this.textBoxContactPersonName.Text;
                    sup.City = this.textBoxCity.Text;
                    sup.SupplierPhoneNumber = this.textBoxPhoneNumber.Text;
                    sup.SupplierFax = this.textBoxFaxNumber.Text;
                    sup.SupplierAddress = this.textBoxAddress.Text;
                    sup.OtherInformation = this.textBoxOtherInfo.Text;
                    sup.SupplierEmailAddress = this.textBoxEmailAddress.Text;
                    dbContext.AddToSuppliers(sup);
                    dbContext.SaveChanges();

                    MessageBox.Show("New Record Inserted");

                    this.textBoxCompanyName.Text = "";
                    this.textBoxContactPersonName.Text = "";
                    this.textBoxCity.Text = "";
                    this.textBoxPhoneNumber.Text = "";
                    this.textBoxFaxNumber.Text = "";
                    this.textBoxAddress.Text = "";
                    this.textBoxOtherInfo.Text = "";
                    this.textBoxEmailAddress.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Some Error Occured");
                }

            }
            else {
                int id = int.Parse(selectedRecord.Cells["ID"].Value.ToString());
                Supplier sup = (from s in dbContext.Suppliers where s.ID == id select s).FirstOrDefault();

                sup.CompanyName = this.textBoxCompanyName.Text;
                sup.ContactPersonName = this.textBoxContactPersonName.Text;
                sup.City = this.textBoxCity.Text;
                sup.SupplierPhoneNumber = this.textBoxPhoneNumber.Text;
                sup.SupplierFax = this.textBoxFaxNumber.Text;
                sup.SupplierAddress = this.textBoxAddress.Text;
                sup.OtherInformation = this.textBoxOtherInfo.Text;
                sup.SupplierEmailAddress = this.textBoxEmailAddress.Text;

                dbContext.SaveChanges();

                this.Close();

            }
        }

        private void AddEditSupplier_Load(object sender, EventArgs e)
        {
            try
            {
                this.textBoxCompanyName.Text = selectedRecord.Cells["CompanyName"].Value.ToString();
            }
            catch { }
            try
            {
            this.textBoxContactPersonName.Text = selectedRecord.Cells["ContactPersonName"].Value.ToString();
            }
            catch { }
            try
            {
            this.textBoxCity.Text = selectedRecord.Cells["City"].Value.ToString();
             }
            catch { }
             try
            {
            this.textBoxPhoneNumber.Text = selectedRecord.Cells["SupplierPhoneNumber"].Value.ToString();
            }
            catch { }
            try
            {
            this.textBoxFaxNumber.Text = selectedRecord.Cells["SupplierFax"].Value.ToString();
            }
            catch { }
            try
            {
            this.textBoxAddress.Text = selectedRecord.Cells["SupplierAddress"].Value.ToString();
            }
            catch { }
            try
            {
            this.textBoxOtherInfo.Text = selectedRecord.Cells["OtherInformation"].Value.ToString();
            }
            catch { }
            try
            {
                this.textBoxEmailAddress.Text = selectedRecord.Cells["SupplierEmailAddress"].Value.ToString();
            }
            catch { }
                    
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
